import sys
import re
import random

sys.path.append('./')
sys.path.append('/Users/xuchaosheng/anaconda3/lib/python3.6/site-packages')

from config import *

client = MongoClient('127.0.0.1', 27017, connect = False)

foreignEnterprise_db = client['foreignEnterprise_db']
foreignEnterprise_coll = foreignEnterprise_db['foreignEnterprise_coll']


@app.route('/trainPlatform/foreignEnterprise/search', methods = ['GET'])
def foreignEnterprise_search():
    kw = request.args.get('kw', '').strip()

    data = [i['name'] for i in foreignEnterprise_coll.find({'name': {'$regex': kw, '$options': 'i'}})]

    data.sort()

    resp = {
        'success': True,
        'data': data[:2000]
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/foreignEnterprise/delete', methods = ['GET'])
def foreignEnterprise_delete():
    name = request.args.get('name', '').strip()

    if not name:
        resp = {
            'success': False,
            'msg': '企业名称不能为空'
        }

        response = make_response(jsonify(resp))
        return response

    r = foreignEnterprise_coll.delete_one({'name': name})

    if r and r.deleted_count:
        resp = {
            'success': True,
            'msg': '删除成功'
        }

        response = make_response(jsonify(resp))
        return response
    else:
        resp = {
            'success': False,
            'msg': '系统繁忙，请您稍后再试'
        }

        response = make_response(jsonify(resp))
        return response


@app.route('/trainPlatform/foreignEnterprise/add', methods = ['GET'])
def foreignEnterprise_add():
    name = request.args.get('name', '').strip()

    if not name:
        resp = {
            'success': False,
            'msg': '企业名称不能为空'
        }

        response = make_response(jsonify(resp))
        return response

    if foreignEnterprise_coll.find_one({'name': name}):
        resp = {
            'success': False,
            'msg': '该企业已存在库中'
        }

        response = make_response(jsonify(resp))
        return response

    foreignEnterprise_coll.insert_one({'name': name})

    resp = {
        'success': True,
        'msg': '成功插入'
    }

    response = make_response(jsonify(resp))
    return response


@app.route('/trainPlatform/foreignEnterprise/update', methods = ['GET'])
def foreignEnterprise_update():
    oldname = request.args.get('oldname', '').strip()
    newname = request.args.get('newname', '').strip()

    if not oldname or not newname:
        resp = {
            'success': False,
            'msg': '企业名称不能为空'
        }

        response = make_response(jsonify(resp))
        return response

    if not foreignEnterprise_coll.find_one({'name': oldname}):
        resp = {
            'success': False,
            'msg': '库中找不到' + oldname
        }

        response = make_response(jsonify(resp))
        return response

    foreignEnterprise_coll.update_one({'name': oldname}, {'$set': {
        'name': newname
    }})

    resp = {
        'success': True,
        'msg': '编辑成功'
    }

    response = make_response(jsonify(resp))
    return response
